import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';

/* 图片缓存组件 */
class CachedImageWidget extends StatelessWidget {
  final double width;
  final double height;
  final String url;
  final BoxFit? fit;
  final BorderRadius? borderRadius;

  const CachedImageWidget(this.width, this.height, this.url, {super.key, this.fit = BoxFit.cover, this.borderRadius});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width,
      height: height,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: borderRadius,
        color: Colors.grey[350],
      ),
      clipBehavior: borderRadius != null ? Clip.antiAlias : Clip.none,
      child: CachedNetworkImage(
        imageUrl: url,
        fit: fit,
        width: width,
        height: height,
        placeholder: (BuildContext context, String url) {
          return Container(
            width: width,
            height: height,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              borderRadius: borderRadius,
              color: Colors.grey[350],
            ),
            clipBehavior: borderRadius != null ? Clip.antiAlias : Clip.none,
            child: const Text(
              "加载中",
              style: TextStyle(fontSize: 16, color: Colors.white),
            ),
          );
        },
      ),
    );
  }
}
